import { useRequest } from "ahooks";
import { Button, Switch, Form, Input, Radio, message, Spin } from "antd";
import { getQuery } from "../../../framework/common/utils";
import request from "../../../framework/common/utils/request";

function getSuperApp() {
  return new Promise((resolve) => {
    request({
      url: "/platform/setting/get-setting?token="+getQuery().token+"&type=superApp",
      method: "post",
    }).then(
        function (data) {
          let returnData = {
            channel_id: "1070",
            api_token: "483febe4-fc29-11ec-894e-fa163e9dbc38",
            app_code: "workflow",
            model: 2
          }//超级APP表单默认值
          if(data.data){
            Object.keys(returnData).forEach((configKey)=>{
              let resultValue = data.data[configKey];//后端返回的值
              if(resultValue === undefined || resultValue === null || resultValue === ""){//如果后端返回的值不存在或者为空
                data.data[configKey] = returnData[configKey];//用默认值替换
              }
            })
            returnData = data.data;
          }
          resolve(returnData)
        },
        function (error) {
          message.error({
            content: error?.message || "未知错误",
          });
        }
    );

  });
}

export default function () {
  const { data, error, loading } = useRequest(getSuperApp, { defaultParams: [getQuery("string")] });
  if (loading) {
    return (
      <div className="flex items-center justify-center h-full">
        <Spin tip="加载中..."></Spin>
      </div>
    );
  }
  const onFinish = (values) => {
    request({
      url: "/platform/setting/save-setting?token="+getQuery().token+"&type=superApp",
      method: "post",
      data:values
    }).then(
      function (data) {
        message.success("保存成功");
      },
      function (error) {
        message.error({
          content: error?.message || "未知错误",
        });
      }
    );
  };

  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <Form
      className="flex flex-col justify-between h-full overflow-y-auto"
      name="basic"
      labelCol={{
        flex: "120px",
      }}
      initialValues={data}
      labelAlign="left"
      colon={false}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <div>
        <Form.Item label="启用" name="enable" valuePropName="checked">
          <Switch />
        </Form.Item>

        <Form.Item label="地址" name="address" rules={[{ required: true, message: "请输入地址" }]}>
          <Input placeholder="请输入地址" />
        </Form.Item>

        <Form.Item label="channelId" name="channel_id" rules={[{ required: true, message: "请输入channelId" }]}>
          <Input placeholder="请输入channelId" />
        </Form.Item>
        <Form.Item label="apiToken" name="api_token" rules={[{ required: true, message: "请输入apiToken" }]}>
          <Input placeholder="请输入apiToken" />
        </Form.Item>
        <Form.Item label="appCode" name="app_code" rules={[{ required: true, message: "请输入appCode" }]}>
          <Input placeholder="请输入appCode" />
        </Form.Item>
        <Form.Item label="消息入口" name="model" rules={[{ required: true, message: "选择消息入口" }]}>
          <Radio.Group>
            <Radio value={1}> 消息模式 </Radio>
            <Radio value={2}> 首页模式 </Radio>
          </Radio.Group>
        </Form.Item>

        <Form.Item label="应用主页">
          <span>{data?.bpm_site_address}/mobile/superapp/auth?__app_code=bpm&from=super_third</span>
        </Form.Item>
      </div>
      <div className="text-center">
        <Button type="primary" htmlType="submit">
          保存
        </Button>
      </div>
    </Form>
  );
}
